package com.ruoyi.adapayBalance.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.adapayBalanceRecord.domain.AdapayBalanceRecord;
import com.ruoyi.adapayBalanceRecord.service.IAdapayBalanceRecordService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.adapayBalance.mapper.AdapayBalanceMapper;
import com.ruoyi.adapayBalance.domain.AdapayBalance;
import com.ruoyi.adapayBalance.service.IAdapayBalanceService;

/**
 * 余额记录Service业务层处理
 *
 * @author ch
 * @date 2022-02-28
 */
@Service
public class AdapayBalanceServiceImpl implements IAdapayBalanceService
{
    @Autowired
    private AdapayBalanceMapper adapayBalanceMapper;
    @Autowired
    private IAdapayBalanceRecordService adapayBalanceRecordService;

    /**
     * 查询余额记录
     *
     * @param id 余额记录主键
     * @return 余额记录
     */
    @Override
    public AdapayBalance selectAdapayBalanceById(Long id)
    {
        return adapayBalanceMapper.selectAdapayBalanceById(id);
    }

    /**
     * 查询余额记录列表
     *
     * @param adapayBalance 余额记录
     * @return 余额记录
     */
    @Override
    public List<AdapayBalance> selectAdapayBalanceList(AdapayBalance adapayBalance)
    {
        return adapayBalanceMapper.selectAdapayBalanceList(adapayBalance);
    }

    /**
     * 新增余额记录
     *
     * @param adapayBalance 余额记录
     * @return 结果
     */
    @Override
    public int insertAdapayBalance(AdapayBalance adapayBalance)
    {
        return adapayBalanceMapper.insertAdapayBalance(adapayBalance);
    }

    /**
     * 修改余额记录
     *
     * @param adapayBalance 余额记录
     * @return 结果
     */
    @Override
    public int updateAdapayBalance(AdapayBalance adapayBalance)
    {
        adapayBalance.setUpdateTime(DateUtils.getNowDate());
        return adapayBalanceMapper.updateAdapayBalance(adapayBalance);
    }

    /**
     * 批量删除余额记录
     *
     * @param ids 需要删除的余额记录主键
     * @return 结果
     */
    @Override
    public int deleteAdapayBalanceByIds(Long[] ids)
    {
        return adapayBalanceMapper.deleteAdapayBalanceByIds(ids);
    }


    /**
     * 批量逻辑删除余额记录
     *
     * @param ids 需要删除的余额记录主键
     * @return 结果
     */
    @Override
    public int logicDeleteAdapayBalance(Long[] ids,String delType,Long delUser)
    {
        return adapayBalanceMapper.logicDeleteAdapayBalance(ids,delType,delUser);
    }

    /**
     * 删除余额记录信息
     *
     * @param id 余额记录主键
     * @return 结果
     */
    @Override
    public int deleteAdapayBalanceById(Long id)
    {
        return adapayBalanceMapper.deleteAdapayBalanceById(id);
    }

    public AdapayBalance getAdapayBalanceByStoreId(Long storeId){
        AdapayBalance adapayBalanceQ=new AdapayBalance();
        adapayBalanceQ.setStoreId(storeId);
        List<AdapayBalance> balanceList=  selectAdapayBalanceList(adapayBalanceQ);
        if(balanceList.size()==1){
         return  balanceList.get(0);
        }else{
            return null ;
        }
    }
    /**
     *
     * @param storeId
     * @param money
     * @param hanleType 加法add 减法sub
     */
    public void handleBalance(Long storeId, BigDecimal money, String hanleType,String handleName,String techRemark,String busType){
        if(storeId==null){
            storeId=0L;
        }
        AdapayBalanceRecord adapayBalanceRecord=new AdapayBalanceRecord();
        adapayBalanceRecord.setStoreId(storeId);
        adapayBalanceRecord.setMoney(money);
        adapayBalanceRecord.setHandleType(hanleType);
        adapayBalanceRecord.setName(handleName);
        adapayBalanceRecord.setBusType(busType);
        adapayBalanceRecord.setTechRemark(techRemark);
        adapayBalanceRecordService.insertAdapayBalanceRecord(adapayBalanceRecord);

        Map<String,Object> map=new HashMap<String,Object>();
        map.put("storeId",storeId);
        map.put("money",money);
        map.put("hanleType",hanleType);
        adapayBalanceMapper.handleBalance(map);

    }
}
